﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Com.BU;
using Com.DA;
using ColdStorage.DOL;
using System.Media;
using System.Windows.Controls.Primitives;

namespace ColdStorage.GUI
{
    /// <summary>
    /// Interaction logic for ucRoomCold.xaml
    /// </summary>
    public partial class ucRoomCold : UserControl
    {

        double roomVolume;
        double roomArea;
        public string key="";

        Room ro;

        /// Delegate truyền dữ liệu Product ID qua cho product để lấy được enthalpy
        public delegate void GetProductId(int iProductId);
        public GetProductId MyProudctId;

        /// Deleegate truyen dữ liệu product type
        public delegate void GetProductType(int iProducttype);
        public GetProductType myProductType;

        /// Delegate truyền diện tích buồng
        public delegate void GetRoomArea(double dbRoomArea);
        public GetRoomArea MyRoomArea;

        // Delegate truyền dung tích buồng
        public delegate void GetRoomCapacity(double dbRoomCapacity);
        public GetRoomCapacity MyRoomCapacity;

        /// DElegate truyền ID room_type
        public delegate void GetRoomTypeID(int iRoomTypeID);
        public GetRoomTypeID MyRoomTypeId;

        //Delegate truyền đối tượng product
        public delegate void GetRoomCold(Room room, bool update);
        public GetRoomCold myRoomCold;

        //Delegate xóa Room
        public delegate void DeleteRoom();
        public DeleteRoom DelRoom;

        public ucRoomCold()
        {
            InitializeComponent();
            loadRoomColdInfo();            
        }

        #region LOAD
        public void loadRoomColdInfo()
        {
            //Load Room Type
            cbxRoomType.Items.Clear();            
            cbxRoomType.ItemsSource = LibraryManagerInCom.RoomTypeList;
            cbxRoomType.DisplayMemberPath = "storage_room";
            cbxRoomType.SelectedIndex = -1;

            // Load Product Name
            cbxInsideStorage.Items.Clear();            
            cbxInsideStorage.ItemsSource = LibraryManagerInCom.ProductList;
            cbxInsideStorage.DisplayMemberPath = "product_name";
            cbxInsideStorage.SelectedIndex = -1;
        }

        public void loadMaxTemp(string maxtemp)
        {
            txtroomOutsideTemp.Text = maxtemp;
        }
        #endregion

        #region PROCESS ROOM
        public void loadroom(Room room)
        {
            if (ro != null)
            {
                ApplyRoom();
            }
            ro = room;
            cbxRoomType.SelectedIndex = ro.iRoomType;
            cbxInsideStorage.SelectedIndex = ro.iInsideStorage;
            txtroomLenght.Text = ro.dbRoomLength.ToString();
            txtroomWidth.Text = ro.dbRoomWidth.ToString();
            txtroomHeight.Text = ro.dbRoomHeight.ToString();
            txtroomCapacity.Text = ro.dbRoomCapacity.ToString();
            txtroomOutsideTemp.Text = ro.dbRoomOutsideTemp.ToString();
            txtLadingCoefficient.Text = ro.dbRoomLadingCoefficient.ToString();
        }

        public void EnableRoom(bool b)
        {
            btnApplyRoom.IsEnabled = b;
            btnCopyRoom.IsEnabled = b;
            btnDeleteRoom.IsEnabled = b;
            CanvasRoom.IsEnabled = b;
        }

        private void cbxInsideStorage_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            tb_product product = (tb_product)cbxInsideStorage.SelectedItem;
            if (product != null)
            {
                tb_loading_standard sd = LibraryManagerInCom.LoadingStandardList.SingleOrDefault
                   (s => s.product_id == product.id);
                if (sd != null)
                {
                    txtLadingCoefficient.Text = sd.loading_standard.ToString();
                }
                MyProudctId(int.Parse(product.id.ToString()));
                myProductType(Convert.ToInt32(product.product_type));
                loadCapacity();
                btnApplyRoom.IsEnabled = true;
            }
        }

        public void loadCapacity()
        {
            try
            {
                Room rRoom = new Room();
                rRoom.dbRoomHeight = Convert.ToDouble(txtroomHeight.Text);
                rRoom.dbRoomWidth = Convert.ToDouble(txtroomWidth.Text);
                rRoom.dbRoomLength = Convert.ToDouble(txtroomLenght.Text);
                rRoom.dbRoomLadingCoefficient = Convert.ToDouble(txtLadingCoefficient.Text);

                roomArea = rRoom.CalculateRoomArea();
                roomVolume = rRoom.CalculateRoomVolume();
                txtroomCapacity.Text = rRoom.CalculateRoomCapacity().ToString();
                rRoom.dbRoomCapacity = Convert.ToDouble(txtroomCapacity.Text);
                // dien tich phong
                MyRoomArea(rRoom.CalculateRoomArea());
                MyRoomCapacity(rRoom.CalculateRoomCapacity());

            }
            catch (Exception)
            {
            }
        }

        private void cbxRoomType_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbxRoomType.SelectedIndex != -1)
            {
                MyRoomTypeId(int.Parse(((tb_storage_room_type)cbxRoomType.SelectedItem).id.ToString()));
                btnApplyRoom.IsEnabled = true;
            }
        }
        #endregion

        #region PROCESS BUTTON

        private void btnnewRoom_Click(object sender, RoutedEventArgs e)
        {
            Room ro = new Room();
            ro.dbRoomOutsideTemp = Convert.ToDouble(txtroomOutsideTemp.Text);
            myRoomCold(ro, false);
        }

        private void btnApplyRoom_Click(object sender, RoutedEventArgs e)
        {
            ApplyRoom();
        }

        public void ApplyRoom()
        {
            if (ro != null)
            {
                ro.iRoomType = cbxRoomType.SelectedIndex;
                ro.dbRoomOutsideTemp = Convert.ToDouble(txtroomOutsideTemp.Text);
                ro.iInsideStorage = cbxInsideStorage.SelectedIndex;
                ro.dbRoomLadingCoefficient = Convert.ToDouble(txtLadingCoefficient.Text);
                ro.dbRoomLength = Convert.ToDouble(txtroomLenght.Text);
                ro.dbRoomWidth = Convert.ToDouble(txtroomWidth.Text);
                ro.dbRoomHeight = Convert.ToDouble(txtroomHeight.Text);
                loadCapacity();
                ro.dbRoomCapacity = Convert.ToDouble(txtroomCapacity.Text);
                ro.dbRoomArea = roomArea;
                ro.dbRoomVolume = roomVolume;
                myRoomCold(ro, true);
                btnApplyRoom.IsEnabled = false;
            }
        }

        private void btnCopyRoom_Click(object sender, RoutedEventArgs e)
        {
            if (ro != null)
            {
                Room copyroom = (Room)ro.Clone();
                myRoomCold(copyroom, false);
            }
        }

        private void btnDeleteRoom_Click(object sender, RoutedEventArgs e)
        {
            if (DelRoom != null)
            {
                DelRoom();
            }
        }
        #endregion

        #region CHECK EMPTY
        public void CheckEmpty(TextBox txt)
        {
            if (txt.Text == "")
            {
                txt.Text = "0";
            }
            if (txt.Text.Contains(' '))
            {
                txt.Text = txt.Text.Remove(txt.Text.IndexOf(' '), 1);
                SystemSounds.Beep.Play();
            }
            if (txt.Text.First() == '.')
            {
                txt.Text = txt.Text.Remove(0, 1);
                SystemSounds.Beep.Play();
            }
        }

        private void txtroomOutsideTemp_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtroomOutsideTemp);
        }

        private void txtLadingCoefficient_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtLadingCoefficient);
            loadCapacity();
        }

        private void txtroomLenght_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtroomLenght);
            loadCapacity();
        }

        private void txtroomWidth_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtroomWidth);
            loadCapacity();
        }

        private void txtroomHeight_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtroomHeight);
            loadCapacity();
        }

        private void txtroomCapacity_TextChanged(object sender, TextChangedEventArgs e)
        {
            CheckEmpty(txtroomCapacity);
        }
        #endregion

        #region CHECK INPUT
        private void txtInput_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            if (Char.IsDigit((Char)e.Text[0]))
            {
                e.Handled = false;
                btnApplyRoom.IsEnabled = true;
            }
            else
            {
                if ((Char)e.Text[0] == '.')
                {
                    if (!((TextBox)sender).Text.Contains("."))
                    {
                        e.Handled = false;
                        btnApplyRoom.IsEnabled = true;
                    }
                    else
                    {
                        e.Handled = true;
                        SystemSounds.Beep.Play();
                    }
                }
                else
                {
                    e.Handled = true;
                    SystemSounds.Beep.Play();
                }
            }
        }
        #endregion

        #region GOT FOCUS
        private void txtInput_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox txt = (TextBox)sender;
            key = txt.Name;
        }

        private void txtInput_Loaded(object sender, RoutedEventArgs e)
        {
            ToolTip tt = new ToolTip();
            TextBox txt = (TextBox)sender;
            ToolTipService.SetPlacement(tt, PlacementMode.Left);
            tt.Content = "Press F1 for Help";
            txt.ToolTip = tt;
        }
        #endregion
    }
}
